import type { ID, Timestamp } from './global';
import type { SearchResult, ContentType } from './search';

// 推荐类型
export type RecommendationType = 
  | 'trending' 
  | 'personalized' 
  | 'similar' 
  | 'collaborative' 
  | 'content_based'
  | 'hybrid';

// 推荐结果
export interface RecommendationResult {
  id: ID;
  type: RecommendationType;
  title: string;
  description: string;
  url: string;
  thumbnail?: string;
  contentType: ContentType;
  score: number; // 推荐分数 0-1
  confidence: number; // 置信度 0-1
  reason: string; // 推荐理由
  tags: string[];
  metadata?: Record<string, any>;
  createdAt: Timestamp;
  expiresAt?: Timestamp;
}

// 推荐上下文
export interface RecommendationContext {
  userId?: ID;
  sessionId: string;
  currentQuery?: string;
  searchHistory: string[];
  clickHistory: ID[];
  userPreferences: {
    categories: string[];
    contentTypes: ContentType[];
    languages: string[];
  };
  contextualInfo: {
    timeOfDay: 'morning' | 'afternoon' | 'evening' | 'night';
    dayOfWeek: string;
    location?: {
      country: string;
      region: string;
    };
    device: 'desktop' | 'mobile' | 'tablet';
  };
}

// 推荐请求
export interface RecommendationRequest {
  userId?: ID;
  type: RecommendationType[];
  limit: number;
  context: RecommendationContext;
  filters?: {
    contentTypes?: ContentType[];
    categories?: string[];
    languages?: string[];
    minScore?: number;
    excludeIds?: ID[];
  };
}

// 推荐响应
export interface RecommendationResponse {
  recommendations: RecommendationResult[];
  metadata: {
    totalCount: number;
    algorithms: string[];
    processingTime: number;
    cacheHit: boolean;
  };
  debug?: {
    scores: Record<string, number>;
    features: Record<string, any>;
    modelVersion: string;
  };
}

// 热门内容
export interface TrendingContent {
  id: ID;
  title: string;
  description: string;
  url: string;
  thumbnail?: string;
  contentType: ContentType;
  category: string;
  viewCount: number;
  shareCount: number;
  likeCount: number;
  trendingScore: number;
  growth: number; // 增长率
  rank: number;
  tags: string[];
  publishedAt: Timestamp;
  trendingAt: Timestamp;
}

// 个性化内容
export interface PersonalizedContent {
  id: ID;
  userId: ID;
  content: SearchResult;
  personalizedScore: number;
  reasons: PersonalizationReason[];
  generatedAt: Timestamp;
  viewedAt?: Timestamp;
  clickedAt?: Timestamp;
  dismissed?: boolean;
}

// 个性化原因
export interface PersonalizationReason {
  type: 'search_history' | 'click_behavior' | 'category_preference' | 'similar_users' | 'trending';
  description: string;
  weight: number;
  evidence?: string[];
}

// 相似内容
export interface SimilarContent {
  id: ID;
  sourceId: ID; // 原始内容ID
  similarContent: SearchResult;
  similarityScore: number;
  similarityType: 'content' | 'category' | 'tags' | 'user_behavior';
  features: string[]; // 相似特征
}

// 推荐算法配置
export interface RecommendationAlgorithmConfig {
  name: string;
  type: RecommendationType;
  enabled: boolean;
  weight: number; // 在混合推荐中的权重
  parameters: Record<string, any>;
  version: string;
  lastUpdated: Timestamp;
}

// 推荐性能指标
export interface RecommendationMetrics {
  algorithmName: string;
  period: {
    start: Timestamp;
    end: Timestamp;
  };
  metrics: {
    impressions: number;
    clicks: number;
    ctr: number; // 点击率
    conversions: number;
    conversionRate: number;
    avgScore: number;
    coverage: number; // 覆盖率
    diversity: number; // 多样性
    novelty: number; // 新颖性
  };
}

// 用户反馈
export interface RecommendationFeedback {
  id: ID;
  userId: ID;
  recommendationId: ID;
  type: 'like' | 'dislike' | 'not_interested' | 'irrelevant' | 'inappropriate';
  reason?: string;
  timestamp: Timestamp;
}

// A/B 测试配置
export interface RecommendationABTest {
  id: ID;
  name: string;
  description: string;
  status: 'draft' | 'running' | 'paused' | 'completed';
  variants: Array<{
    name: string;
    traffic: number; // 流量分配百分比
    config: RecommendationAlgorithmConfig;
  }>;
  startDate: Timestamp;
  endDate: Timestamp;
  metrics: RecommendationMetrics[];
}
